home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / ZODB / lock_file.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  2.1 KB  |  75 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import os
  5. import errno
  6. import logging
  7. logger = logging.getLogger('ZODB.lock_file')
  8.  
  9. try:
  10.     import fcntl
  11. except ImportError:
  12.     
  13.     try:
  14.         from winlock import LockFile as _LockFile
  15.         from winlock import UnlockFile as _UnlockFile
  16.     except ImportError:
  17.         
  18.         def lock_file(file):
  19.             logger.info('No file-locking support on this platform')
  20.  
  21.  
  22.     
  23.     def lock_file(file):
  24.         _LockFile(file.fileno(), 0, 0, 1, 0)
  25.  
  26.     
  27.     def unlock_file(file):
  28.         _UnlockFile(file.fileno(), 0, 0, 1, 0)
  29.  
  30.  
  31. _flags = fcntl.LOCK_EX | fcntl.LOCK_NB
  32.  
  33. def lock_file(file):
  34.     fcntl.flock(file.fileno(), _flags)
  35.  
  36.  
  37. def unlock_file(file):
  38.     pass
  39.  
  40.  
  41. class LockFile:
  42.     
  43.     def __init__(self, path):
  44.         self._path = path
  45.         
  46.         try:
  47.             self._fp = open(path, 'r+')
  48.         except IOError:
  49.             e = None
  50.             if e.errno != errno.ENOENT:
  51.                 raise 
  52.             
  53.             self._fp = open(path, 'w+')
  54.  
  55.         
  56.         try:
  57.             lock_file(self._fp)
  58.         except:
  59.             logger.exception('Error locking file %s', path)
  60.             raise 
  61.  
  62.         print >>self._fp, os.getpid()
  63.         self._fp.flush()
  64.  
  65.     
  66.     def close(self):
  67.         if self._fp is not None:
  68.             unlock_file(self._fp)
  69.             self._fp.close()
  70.             os.unlink(self._path)
  71.             self._fp = None
  72.         
  73.  
  74.  
  75.